package com.itshare.video.controller;

import com.itshare.video.common.annontation.AttrData;
import com.itshare.video.common.core.constant.enums.ApiInfo;
import com.itshare.video.common.core.validator.UpdateGroup;
import com.itshare.video.common.dto.Result;
import com.itshare.video.common.dto.ViewObject;
import com.itshare.video.common.util.JwtAdapter;
import com.itshare.video.mapper.UserRoleMapper;
import com.itshare.video.pojo.dto.EmailLoginDTO;
import com.itshare.video.pojo.dto.RegisterDTO;
import com.itshare.video.pojo.dto.UserInfoDTO;
import com.itshare.video.pojo.dto.UserTokenDTO;
import com.itshare.video.pojo.po.User;
import com.itshare.video.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author lyr
 * @create 2020/10/24 9:47
 */
@Slf4j
@RestController
@RequestMapping("/api/")
public class UserLoginController {

    @RequestMapping("/ping")
    public String ping() {
        return "OK";
    }

    // @RequestMapping("/getToken")
    // public String token() {
    //     return JwtAdapter.createJWT("444", Duration.ofDays(30).toMillis());
    // }


    @Resource
    UserService userService;
    @Resource
    private UserRoleMapper userRoleMapper;



    /**
     * 用户登录接口
     * @param loginDTO 登录信息
     * @return
     */
    @PostMapping("/login/email")
    public ViewObject emailLogin(@RequestBody @Validated EmailLoginDTO loginDTO) {
        User user = userService.login(loginDTO);
        if(user==null) {
            return ViewObject.ofCode(ApiInfo.NO_RESOURCE).setMessage("没有该账户");
        }
        log.info("user {}",user);


        Map<String, Object> map = new HashMap<>();
        map.put("id",user.getUserId());
        map.put("role",user.getRoleId());
        String token = JwtAdapter.createJWT(map,Duration.ofDays(2).toMillis());
        //返回 jwt token 给前端
        return ViewObject.of(ApiInfo.OK)
                .put("token",token)
                .put("role_id",user.getRoleId())
                .put("nickname",user.getNickname())
                .put("username",user.getUserName());
    }


    /**
     * 用户注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/register/email")
    public Result register(@RequestBody @Validated RegisterDTO registerDTO) {
        return userService.register(registerDTO);
    }


    @GetMapping("/userInfo/{userId}")
    public Result<?> getUserInfoById(@AttrData UserTokenDTO userTokenDTO, @PathVariable Long userId) {
        //获取用户的基本信息

        return userService.getUserInfo(userId);
    }


    @GetMapping("/userInfo")
    public Result<?> getUserInfo(@AttrData UserTokenDTO userTokenDTO) {
        //获取用户信息
        log.info("{}",userTokenDTO);
        return userService.getUserInfo(userTokenDTO.getId().longValue());
    }


    /**
     * 更新用户信息
     * @return
     */
    @PutMapping("/userInfo")
    public ViewObject updateUserInfo(@RequestBody @Validated(UpdateGroup.class) UserInfoDTO userInfoDTO, @AttrData UserTokenDTO userTokenDTO) {
        // userService.  获取用户的信息
        return userService.updateUserInfo(userInfoDTO,userTokenDTO);
    }

















}
